package conf2;

import conf2.iface.IGamePieceListener;
import conf2.iface.MoveListener;
import conf2.model.EActor;
import conf2.model.EActor.Side;
import conf2.model.ETerritory;
import conf2.model.GameState;
import conf2.model.Move;
import conf2.model.MoveLogic;
import conf2.model.WorldState;
import conf2.text.Actors;
import conf2.ui.ActorInfoGlassPane;
import conf2.ui.GamePiece;
import conf2.ui.MainWindow;
import conf2.ui.PieceCollection;
import conf2.ui.Portraits;
import conf2.ui.RegionBounds;
import java.awt.Image;
import java.awt.Point;
import java.util.LinkedList;
import java.util.List;

public class PieceHandler implements IGamePieceListener
{

    private final MainWindow mw;
    private final WorldState ws;
    private final GameState gs;
    private final PieceCollection pieceColl;
    private final Configuration config;
    private boolean isHighlightTriggered = false;
    /** Is only set for enemies. We don't trigger hovers on friendlies. */
    private GamePiece hoveredPiece = null;
    private final MoveListener moveListener;

    public PieceHandler(MainWindow mw, Configuration config, WorldState ws, GameState gs, PieceCollection pieceColl, MoveListener ml)
    {
        this.mw = mw;
        this.ws = ws;
        this.gs = gs;
        this.pieceColl = pieceColl;
        this.config = config;
        this.moveListener = ml;
    }

    public void pieceDropped(GamePiece source, Point where)
    {
        EActor who = source.getActor();
        ETerritory from = ws.whereIs(who);
        ETerritory to = RegionBounds.TerritoryIdAt(where);


        if (MoveLogic.isValidMove(ws, who, to)) {
            EActor defender = null;
            // Over somebody?
            if (hoveredPiece != null) {
                defender = hoveredPiece.getActor();
                assert defender.getOpposingSide() == who.getSide();
            }
            Move m = new Move(who, defender, from, to);

            if (defender != null) {
                moveListener.attackMoveMade(m);
            } else {
                moveListener.moveMade(m);
            }

        } else {
            source.snapback();
        }
    }

    public void pieceClicked(GamePiece source, boolean doubleClick, int keyModifiers)
    {
        System.out.println("CLICKED");
        EActor who = source.getActor();
        if (config.getMovementAssist() && !isHighlightTriggered) {
            List<ETerritory> validMoves = new LinkedList<ETerritory>();
            for (ETerritory et : ETerritory.values()) {
                if (MoveLogic.isValidMove(ws, who, et)) {
                    validMoves.add(et);
                }
            }

            mw.highlightRegions(validMoves);

            isHighlightTriggered = true;
        }
    }

    public void pieceAltClicked(GamePiece source, boolean doubleClick, int keyModifiers)
    {
        EActor actor = source.getActor();
        String name = Actors.getName(actor);
        String desc = Actors.getDescription(actor);
        int atkVal = actor.getDefaultAttackValue();
        Image img = Portraits.get(actor);
        Side side = actor.getSide();
        ActorInfoGlassPane.show(mw, name, desc, atkVal, img, side);
    }

    public void pieceDragged(GamePiece source, Point where)
    {
        source.getParent().setComponentZOrder(source, 0);

        EActor who = source.getActor();
        GamePiece over = pieceColl.getPieceAt(where, source);
        if (over != null && over != hoveredPiece) {
            EActor overWho = over.getActor();
            if (overWho.getSide() == who.getOpposingSide()) {
                if (hoveredPiece != null) {
                    hoveredPiece.unmark();
                }
                hoveredPiece = over;
                hoveredPiece.mark();
            }
        } else if (over == null && hoveredPiece != null) {
            hoveredPiece.unmark();
            hoveredPiece = null;
        }
    }

    public void pieceReleased(GamePiece source)
    {
        if (isHighlightTriggered) {
            mw.highlightRegions(null);
            isHighlightTriggered = false;
        }

    }
}
